<?php

namespace app\controllers;

use app\models\UploadFile;
use app\models\UploadForm;
use Yii;
use app\models\Dormchangeinfo;
use app\models\DormchangeinfoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Personinfo;
use yii\web\UploadedFile;

/**
 * DormchangeinfoController implements the CRUD actions for Dormchangeinfo model.
 */
class DormchangeinfoController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Dormchangeinfo models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new DormchangeinfoSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Dormchangeinfo model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Dormchangeinfo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Dormchangeinfo();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Dormchangeinfo model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Dormchangeinfo model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Dormchangeinfo model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Dormchangeinfo the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Dormchangeinfo::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionGetname($stuid)
    {
    	$model=Personinfo::findOne(['stu_id'=>$stuid]);
    	if($model==null)
    		$res='该学号未录入信息列表';
    		else $res=$model->stu_name;
    		return $res;
    		 
    }


    /**
     * 导入excel文件内容
     */
    public function actionImport()
    {
        if (\Yii::$app->request->isPost) {
            $filename=\Yii::$app->request->post('filename');
            $token=\Yii::$app->request->post('token');
            //验证token
            if($token!='fsf#%^#oe,.')
            {
                $message="非法操作！";
                $message=json_encode($message);
                return $message;
            }
            if($filename==null){
                $message="请先上传文件！";
            }else{
                $filename="uploads/excel/".$filename;
                /**
                 * 导入插件
                 */
                if(file_exists($filename)){//如果文件存在,导入并删除
                    $data = \moonland\phpexcel\Excel::widget([
                        'mode' => 'import',
                        'fileName' => $filename,
                        'setFirstRecordAsKeys' => true, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
                        'setIndexSheetByName' => true, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
                        'getOnlySheet' => 'sheet1', // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
                    ]);
                    if(Dormchangeinfo::SaveImport($data)){
                        //移除刚刚上传的文件
                        unlink($filename);
                        $message="导入成功！";
                    }else{
                        $message="导入失败！";
                    }
                }else{
                    $message="文件不存在！";
                }
            }

            $message=json_encode($message);
            return $message;
        } else {
            return $this->render('import', [

            ]);
        }

    }


    /**
     * 上传excel文件
     */
    public function actionUpload()
    {
        /*
         * 下面 $model 是表单模型，$model1是文件模型，文件模型中有属性“excel”
         */
        $model = new UploadForm();   //$model是表单模型
        $model1=new UploadFile();    //$model1是实体模型，需要时可以在控制器里save这个模型的（此处并没有save）
        if (Yii::$app->request->isPost) {
            $model->excel = UploadedFile::getInstance($model1, 'excel');//$model1 是从视图传过来的
            if (($result=$model->upload())) {
                $result=json_encode($result);//要求返回json格式
                return $result;
            }
        }
    }

}
